<?php
namespace app\we\controller;
use think\Controller;
use think\helper\Time;


class Feed extends Controller{


    public function _initialize(){

        $this->rank=array(1=>'好评',2=>'中评',3=>'差评');
        $this->rankico=array(1=>"/:strong",2=>'/:,@f',3=>"/:weak");
      
    }

    public function index($openid=""){
      $fans=model('WeFans')->info($openid);
      if(request()->isPost()){
        $data=input('post.');
        // 数据有效性
        
        $data['aid']=$fans['aid'];
        $data['appid']=$fans['appid'];
        $data['nickname']=$fans['nickname'];
        //计算当日反馈数量
        $today = Time::today();
        $today_total=db('WeFeed')->where('aid',$data['aid'])->where('shopid',$data['shopid'])->where('create_time','between time',$today)->count();
        $data['today_um']=$data['shopid'].'-'.($today_total+1);
        $today_my_total=db('WeFeed')->where('openid',$data['openid'])->where('shopid',$data['shopid'])->where('create_time','between time',$today)->count();
        if ($today_my_total>0) $this->error('您今天已经反馈，谢谢');
        $feed_id=model('we/WeFeed')->editData($data);
        
        if ($feed_id){
        //优先发送客服消息，失败后发送模板消息
            $admins=model('we/WeFans')->getAdmins($fans['appid']);
            $shop=model('admin/AdminShop')->info($data['shopid']);
            $message=$shop['title']."第".($today_total+1).'条客情：';
            $message.="\n".$fans['id']."号顾客 - 评价".$data['target'];
            $message.="\n产品".$this->rankico[$data['product']]." ； 服务".$this->rankico[$data['service']];
            $message.="\n留言(".$feed_id."):\n".$data['content'];
            foreach ($admins as $key => $admin) {
               custom_message($admin['openid'],$message);
            }
            custom_message($fans['openid'],'您的反馈我们已经收到，谢谢！么么哒');
            $this->success('成功反馈');
        }
        $this->error('反馈失败');

      }else{
        $shops=my_select('AdminShop','id','title',['aid'=>$fans['aid']]); 
        
        $this->assign('openid',$openid);
        $this->assign('shops',$shops);
        return $this->fetch();
      }
  
       
    }

    public function chat($feed_id="",$admin_openid='',$openid='',$type=0){

      $feed=model('WeFeed')->info($feed_id);
      if (!$feed) die('回话已经过期');
      if ($openid and $openid!=$feed['openid']) die('无权回复');
      $fans=model('we/WeFans')->info($feed['openid']); //获取了使用者
      if($type==1 and $admin_openid) {
        $admin=model('we/WeFans')->info($admin_openid); //获取了客服
        if (!$admin['admin']) die('未登陆');
        $cards=db('WeCard')->where('appid',$fans['appid'])->select();
        $cards_info='卡券列表：(卡1：发1号卡券给粉丝)';
        foreach ($cards as $key => $card) {
            $cards_info.="</br>".$card['id']."号卡券：".$card['title'];
        }
        $fnas_cards=get_fans_card($fans['appid'],$fans['openid']);
        foreach ($fnas_cards['card_list'] as $key => $fnas_card) {
           $this_card= db('WeCard')->where('card_id',$fnas_card['card_id'])->find();
           $cards_info.="</br>该粉丝拥有</br>".$this_card['id']."号卡券：".$this_card['title'];
        }
      }

    
      
      $chats=db('WeFeedChat')->where('feed',$feed_id)->select();
      if ($chats){
      foreach ($chats as $key => &$chat) {
          $admin=model('we/WeFans')->info($chat['admin_openid']);
          if ($chat['type']==1){
            $chat['headimgurl']=$admin['headimgurl'];
          }else{
            $chat['headimgurl']=$fans['headimgurl'];
          }
      }
      }

      if(request()->isPost()){
        $data=input('post.');
        // 数据有效性
        
        $data['aid']=$fans['aid'];
        $data['feed']=$data['feed_id'];
        $data['nickname']=$fans['nickname'];
        $data['admin_openid']=$admin['openid'];
        $data['admin_nickname']=$admin['nickname'];
        $data['type']=$type;
       
       
        $chat_id=model('we/WeFeedChat')->editData($data);
        
        if ($chat_id){
            if ($type==0){
            $message="顾客回复了客情：";
            $message.="\n".$fans['id']."号顾客 - 评价".$feed['target'];
            $message.="\n产品".$this->rankico[$feed['product']]." ； 服务".$this->rankico[$feed['service']];
            $message.="\n留言(".$feed['id']."):\n".$feed['content'];
            $message.="\n----------------";
            $message.="\n".$fans['nickname'].':'.$data['content'];
            $url=my_domain().url('chat',['feed_id'=>$feed_id,'admin_openid'=>$admin['openid'],'type'=>1]);
            $message.="\n".'<a href="'.$url.'">点击这里回复TA</a>';
            
            $message_error=custom_message($admin['openid'],$message);

            }
            if ($type==1){
            $first=mb_substr($data['content'],0,1); 
           
            if ($first=='卡') {
            $card_id=str_replace('卡',"",$data['content']);
            $card=model('we/weCard')->info($card_id);
            $message=['msgtype'=>'wxcard','wxcard'=>['card_id'=>$card['card_id']]];   
            }else{
            $message=date('m-d H:i:s')."你的反馈有了新的回复：";
            $message.="\n产品".$this->rankico[$feed['product']]." ； 服务".$this->rankico[$feed['service']];
            $message.="\n留言(".$feed['id']."):\n".$feed['content'];
            $message.="\n----------------";
            $message.="\n".$data['admin_nickname'].':'.$data['content'];
            $url=my_domain().url('chat',['feed_id'=>$feed_id,'openid'=>$fans['openid'],'type'=>0]);
            $message.="\n".'<a href="'.$url.'">点击这里回复TA</a>';
            }
            
            $message_error=custom_message($fans['openid'],$message);  
            }
            
            $this->success('成功反馈'.$message_error,url('chat',['feed_id'=>$feed_id,'openid'=>$openid,'admin_openid'=>$admin_openid,'type'=>$type]));
        }
        $this->error('反馈失败');

      }else{

        $this->assign('feed',$feed);
        $this->assign('chats',$chats);
        $this->assign('fans',$fans);
        $this->assign('admin_openid',$admin_openid);
        $this->assign('type',$type);
        if (isset($cards_info))$this->assign('cards_info',$cards_info);
      
        return $this->fetch();
      }
  
       
    }

   
}